package main

import (
	"fmt"
	"go.manyji.com/lib/config"
	"net/http"
	"regexp"
	"sieve_proc/operate"
	"sieve_proc/util"
	"strings"
	"time"

	"go.manyji.com/lib/util/convert"
	uTime "go.manyji.com/lib/util/time"
)

var sieveDebug = config.Bool("sieve.debug", false, "sieve debug")

type mod_api struct{}

func (this *mod_api) API_UploadTaskData(ctx *http_context) interface{} {
	_, file, err := ctx.Http.r.FormFile("file")
	if err != nil {
		return err
	}
	f, err := file.Open()
	if err != nil {
		return err
	}
	list := []string{}
	reg := regexp.MustCompile(`^[0-9]*$`)
	err = util.LoadLines(f, func(p string) error {
		if ok := reg.MatchString(p); !ok {
			return fmt.Errorf("号码格式不对")
		}
		list = append(list, p)
		return nil
	})
	if err != nil {
		return err
	}
	if len(list) == 0 {
		return fmt.Errorf("未发现导入数据")
	}
	op := operate.NewOperator(ctx.Args.Get("op"))
	if op == nil {
		return fmt.Errorf("op参数有误")
	}
	if tag := op.GetTag(); tag == "wsa" || tag == "vibera" || tag == "tga" {
		activeType := ctx.Args.Get("activeType")
		if !(activeType == "0" || activeType == "1" || activeType == "2" || activeType == "-1") {
			return fmt.Errorf("activeType参数有误")
		}
		op.UpdateExtra(activeType)
	}
	oid, err := op.CreateTaskData(ctx.Args.Get("country"), list)
	if err != nil {
		return err
	}
	return map[string]interface{}{
		"task_id":  oid,
		"task_cnt": op.GetTCnt(),
	}
}

func (this *mod_api) API_CheckTask(ctx *http_context) interface{} {
	if ctx.Args.Get("taskId") == "" {
		return fmt.Errorf("参数有误")
	}
	ops := strings.Split(ctx.Args.Get("taskId"), "_")
	if len(ops) != 2 {
		return fmt.Errorf("参数有误")
	}
	op := operate.NewOperator(ops[0])
	if op == nil {
		return fmt.Errorf("参数拼接有误")
	}
	orderInfo, err := op.LoadOrderData(convert.Atoi(ops[1]))
	if err != nil {
		return err
	}
	return map[string]interface{}{
		"status":         orderInfo.Status,
		"count":          orderInfo.TCnt,
		"ok_count":       orderInfo.SCnt,
		"complete_count": orderInfo.FCnt,
		"SError":         orderInfo.Ret,
	}
}

func (this *mod_api) API_DownTaskFile(ctx *http_context) interface{} {
	if ctx.Args.Get("taskId") == "" {
		return fmt.Errorf("参数有误")
	}
	ops := strings.Split(ctx.Args.Get("taskId"), "_")
	if len(ops) != 2 {
		return fmt.Errorf("参数有误")
	}
	op := operate.NewOperator(ops[0])
	if op == nil {
		return fmt.Errorf("参数拼接有误")
	}
	if ctx.Args.Get("file_type") != "txt" && ctx.Args.Get("file_type") != "xlsx" && ctx.Args.Get("file_type") != "csv" {
		return fmt.Errorf("文件类型有误")
	}
	orderInfo, err := op.LoadOrderData(convert.Atoi(ops[1]))
	if err != nil {
		return err
	}
	if orderInfo.Status != 3 {
		ctx.Http.w.WriteHeader(http.StatusNotFound)
		return nil
	}

	fileName := util.GetFilterDir(uTime.FmtDate(time.Unix(orderInfo.CreateTime, 0), "Ymd")) + "/" + orderInfo.GetTagId() + "." + ctx.Args.Get("file_type")
	if *sieveDebug {
		fileName = util.GetFilterDir("debug") + "/" + "debug" + "." + ctx.Args.Get("file_type")
	}
	b, err := util.PathExists(fileName)
	if err != nil {
		return err
	}
	if !b {
		return fmt.Errorf("文件不存在")
	}

	http.ServeFile(ctx.Http.w, ctx.Http.r, fileName)
	return nil
}
